﻿using System;
using System.Data;
using System.Configuration;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Collections;
using System.Text.RegularExpressions;
using System.Data.SqlClient;
using System.Web.UI;
using Dcms.BLL;
using Dcms.Common;
using Dcms.Model;

public partial class Manage_RoleManage_PopedomManage : System.Web.UI.Page
{
    public  CompetenceLib CL = new CompetenceLib();
    Dcms_PerRoleBLL pBll = new Dcms_PerRoleBLL();
    Dcms_CateBLL cBll = new Dcms_CateBLL();
    public int RoleId
    {
        get
        {
            return DcmsRequest.GetQueryInt("RolID", 0);
        }
    }
    public String ROLESTR = "POPEDOM";
    protected void Page_Load(object sender, EventArgs e)
    {
        try
        {
           
            CL.IsLogin();
 
            if (!IsPostBack)
            {
                bindDate();
            }
        }
        catch (Exception ex)
        {
            Log.LogHandler.LogError("Manage_RoleManage_PopedomManage.Page_Load", ex);
            throw ex;
        }
    }
    public void bindDate() {
        if (RoleId > 0)
        {
            this.repPopedom.DataSource = cBll.GetModelListByLanguage("cn");
            this.repPopedom.DataBind();
        }
    }
    protected void repPopedom_ItemDataBound(object sender, System.Web.UI.WebControls.RepeaterItemEventArgs e)
    {
        Literal litCateName = (Literal)e.Item.FindControl("litCateName");
        HiddenField hidCateID = (HiddenField)e.Item.FindControl("hidCateID");
        string strCateIDPath = (string)DataBinder.Eval(e.Item.DataItem, "Cate_IDPath");
        string strCateName = (string)DataBinder.Eval(e.Item.DataItem, "Cate_Name");
        int intCateID = (int)DataBinder.Eval(e.Item.DataItem, "Cate_ID");
        hidCateID.Value = intCateID.ToString();
        string strSpil = "";
        char[] cel = {','};
        string[] arr = strCateIDPath.Split(cel);
        for (int i = 3; i < arr.Length; i++)
        {
            if (i == 3)
            {
                strSpil = "┠";
            }
            else
            {
                strSpil += "─";
            }
        }
        strCateName = strSpil + strCateName;
        litCateName.Text = strCateName;
        


        //pFlag.Value = DataBinder.Eval(e.Item.DataItem, "Permission_Flag").ToString() + RoleId;
        //pID.Value = DataBinder.Eval(e.Item.DataItem, "Permission_Id").ToString();
        //string Image = "";

        //if (Convert.ToInt32(DataBinder.Eval(e.Item.DataItem, "Permission_ParentID")) == 0)
        //{
        //    Image = "<img src=\"../images/skin3/closed.gif\"/>";
        //}
        //else {
        //    Image = " <img src=\"../css/s.gif\" width=\"10\" height=\"1\"/><img src=\"../images/skin3/child.gif\"/>";
        //}

        //pName.Text = Image + DataBinder.Eval(e.Item.DataItem, "Permission_Name");

        CheckBox IsRead = (CheckBox)e.Item.FindControl("chk_IsRead");
        IsRead.Checked = CL.IsRead(intCateID, RoleId);

        CheckBox IsNew = (CheckBox)e.Item.FindControl("chk_IsAdd");
        IsNew.Checked = CL.IsAdd(intCateID, RoleId);

        CheckBox IsEdit = (CheckBox)e.Item.FindControl("chk_IsEdit");
        IsEdit.Checked = CL.IsEdit(intCateID, RoleId);

        CheckBox IsDel = (CheckBox)e.Item.FindControl("chk_IsDelete");
        IsDel.Checked = CL.IsDelete(intCateID, RoleId);
    }
    protected void SetErrorMsg(String Msg)
    {
        Mesg.Text = "<div id=\"msgCard\"><div class=\"error\"><div class=\"error-content\">" + Msg + "</div></div></div>";
    }
    protected void SetOKMsg(String Msg)
    {
        Mesg.Text = "<div id=\"msgCard\"><div class=\"accurate\"><div class=\"accurate-content\">" + Msg + "</div></div></div>";
    }
    protected void btnSave_Click(object sender, EventArgs e)
    {
        Dcms_PerRoleBLL prBll = new Dcms_PerRoleBLL();
        Dcms_PerRoleModel prModel = new Dcms_PerRoleModel();
        for (int i = 0; i < repPopedom.Items.Count; i++)
        {
            CheckBox IsRead = (CheckBox)repPopedom.Items[i].FindControl("chk_IsRead");
            CheckBox IsAdd = (CheckBox)repPopedom.Items[i].FindControl("chk_IsAdd");
            CheckBox IsEdit = (CheckBox)repPopedom.Items[i].FindControl("chk_IsEdit");
            CheckBox IsDelete = (CheckBox)repPopedom.Items[i].FindControl("chk_IsDelete");
            HiddenField hidCateID = (HiddenField)repPopedom.Items[i].FindControl("hidCateID");
            int CateID = Convert.ToInt32(hidCateID.Value);

            prModel = prBll.GetModelByPIdAndRId(CateID, RoleId);

            Dcms_PerRoleModel model = new Dcms_PerRoleModel();
            model.PerRole_IsAdd = 0;
            model.PerRole_IsDelete = 0;
            model.PerRole_IsEdit = 0;
            model.PerRole_IsRead = 0;
            if (IsAdd.Checked)
            {
                model.PerRole_IsAdd = 1;
            }
            if (IsDelete.Checked)
            {
                model.PerRole_IsDelete = 1;
            }
            if (IsEdit.Checked)
            {
                model.PerRole_IsEdit = 1;
            }
            if (IsRead.Checked)
            {
                model.PerRole_IsRead = 1;
            }
            model.PerRole_CId = CateID;
            model.PerRole_RId = RoleId;

            if (prModel != null)
            {
                prBll.Update(model);
            }
            else
            {
                prBll.AddModel(model);
            }
        }
        SetOKMsg("设置角色权限成功！");
    }
}
